package com.example.retailmanagement.controller;

import com.example.retailmanagement.model.Product;
import com.example.retailmanagement.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/products")
public class ProductController {

    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private ProductService productService;

    // 获取所有产品
    @GetMapping
    public ResponseEntity<List<Product>> getAllProducts() {
        logger.info("开始执行获取所有产品的请求");
        List<Product> products = productService.getAllProducts();
        logger.info("成功获取到 {} 个产品", products.size());
        return new ResponseEntity<>(products, HttpStatus.OK);
    }

    // 获取指定产品
    @GetMapping("/{id}")
    public ResponseEntity<Product> getProductById(@PathVariable Long id) {
        logger.info("开始执行获取指定产品的请求，产品ID为：{}", id);
        Product product = productService.getProductById(id);
        if (product!= null) {
            logger.info("成功获取到指定产品，产品信息：{}", product);
            return new ResponseEntity<>(product, HttpStatus.OK);
        } else {
            logger.warn("未找到指定产品，产品ID：{}", id);
            return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
        }
    }

    // 插入新产品
    @PostMapping
    public ResponseEntity<Boolean> insertProduct(@RequestBody Product product) {
        logger.info("开始执行插入新产品的请求，新产品信息：{}", product);
        boolean result = productService.insertProduct(product);
        if (result) {
            logger.info("成功插入新产品：{}", product);
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            logger.error("插入新产品失败，产品信息：{}", product);
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 更新指定产品
    @PutMapping("/{id}")
    public ResponseEntity<Boolean> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        product.setId(id);
        logger.info("开始执行更新指定产品的请求，产品ID：{}，更新后的产品信息：{}", id, product);
        boolean result = productService.updateProduct(product);
        if (result) {
            logger.info("成功更新指定产品，产品ID：{}，更新后的产品信息：{}", id, product);
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            logger.error("更新指定产品失败，产品ID：{}，产品信息：{}", id, product);
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    // 删除产品
    @DeleteMapping("/{id}")
    public ResponseEntity<Boolean> deleteProduct(@PathVariable Long id) {
        logger.info("开始执行删除产品的请求，产品ID：{}", id);
        boolean result = productService.deleteProduct(id);
        if (result) {
            logger.info("成功删除产品，产品ID：{}", id);
            return new ResponseEntity<>(true, HttpStatus.OK);
        } else {
            logger.error("删除产品失败，产品ID：{}",
                    id);
            return new ResponseEntity<>(false, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}